import {BaseNode, type BaseNodeParams} from "./baseNode";

export interface imageNodeParam extends BaseNodeParams{
    url?: string;
    imageData?: string;
}

export class ImageNode extends BaseNode {

    private _url: string;
    private _imageData: string;
    private _image: any;
    private _sourceImageData: string;

    constructor(param) {
        const {url, imageData, ...rst} = param;
        super({...rst, type: 'image'});
        this._url = url ;
        this._imageData = imageData;
        this._sourceImageData = param._sourceImageData
    }

    /**
     * 更新图片源
     * @param param
     */
    updateSrc(param) {
        this._url = param.url;
        this._imageData = param.imageData;
        this.width = param.width;
        this.height = param.height;
        this.x = param.x;
        this.y = param.y;
        this._sourceImageData = param._sourceImageData
        this.updateBox({x: param.x, y: param.y, width: param.width, height: param.height})
    }

    render(ctx) {
        this.renderImageData(ctx, this.x, this.y);
    }

    /**
     * 渲染离屏图片
     */
    renderOffScreenCanvas() {
        const offScreenCtx = super.getOffScreenCanvas();
        this.renderImageData(offScreenCtx, 0, 0);
    }

    public getOffScreenCanvasImage() {
        this.renderOffScreenCanvas();
        console.log(this.getOffScreenCanvasImageMark())
        return this.getOffScreenCanvasImageMark();// .getImageData(this.x, this.y, this.width, this.height);
    }

    renderImageData(ctx: any, x, y): void {
        ctx.save();
        ctx.translate(x + (this.width) / 2, y + this.height / 2);
        ctx.rotate(this.rotation * Math.PI / 180);
        // console.log('this.scale', this.scale, this.scale * this.flipX, this.scale * this.flipY)
        ctx.scale(this.scale * this.flipX, this.scale * this.flipY)
        ctx.drawImage(this._imageData, -this.width/2, -this.height/2, this.width, this.height); // 渲染图片
        ctx.restore();
    }
    /**
     * 设置 http url 图片
     * @param url
     */
    renderImageUrl(url: string): void {
        this._context.drawImage(img, this.x, this.y, this.width,this.height);
    }


}
